package com.example.demo.controller;

import com.example.demo.entity.Blog;
import com.example.demo.entity.Type;
import com.example.demo.mapper.BlogMapper;
import com.example.demo.service.impl.BlogServiceImpl;
import com.example.demo.service.impl.CommentServiceImpl;
import com.example.demo.service.impl.TypeServiceImpl;
import com.example.demo.service.impl.UserServiceImpl;
import com.example.demo.util.IpUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Controller
public class IndexControoler {

    @Autowired
    private BlogServiceImpl blogService;

    @Autowired
    private TypeServiceImpl typeService;

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private CommentServiceImpl commentService;

    @Resource
    ValueOperations<String ,Object> valueOperations;

    @Autowired
    private BlogMapper blogMapper;

    @GetMapping("/")
    public String index(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum, Model model){
        Long userId = 1L;
        PageHelper.startPage(pageNum,10,"createDate desc");
        List<Blog> blogs = blogService.getAllBlog(userId);
        List<Type> types = typeService.getTopType(6);
        List<Blog> recommendedBlogs = blogService.getAllRecommendBlog();
        for(Blog blog : blogs){
            Type type = typeService.getType(blog.getTypeId());
            blog.setType(type);
            //将user的信息给blog
            blog.setUser(userService.findUserById(userId));
        }

        PageInfo<Blog> pageInfo = new PageInfo<>(blogs);

        for(Type type : types){
            List<Blog> blogList = blogService.getAllTypeBlog(type.getId(),userId);
            type.setBlogs(blogList);
        }
        model.addAttribute("types",types);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("recommendedBlogs",recommendedBlogs);
        return "index";
    }

    /*
    * 查看某篇文章
    * */
    @GetMapping("/blog/{id}")
    public String blog(@PathVariable Long id, Model model, HttpSession session, HttpServletRequest request){
        Blog blog = blogService.getAndConvert(id);
        /*在执行浏览次数增加前*/
        String ip = IpUtil.getIpAddr(request);
        System.out.println("----ip-----:"+ip);

        try{
            String val = valueOperations.get(ip).toString();
        }catch (NullPointerException e)
        {
            blog.setViews(blog.getViews()+1);
            blogMapper.updateViews(blog.getViews(),blog.getId());
            //有效期为1个小时,超过则key失效
            valueOperations.set(ip,"true", 1, TimeUnit.HOURS);

        }

        blog.setUser(userService.findUserById(blog.getUserId()));
        blog.setType(typeService.getType(blog.getTypeId()));
        model.addAttribute("blog",blog);
        return "blog";
    }


    @GetMapping("/about")
    public String about(){
        return "about";
    }
}
